package com.example.carbon.demos.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.carbon.demos.web.common.QueryPageParam;
import com.example.carbon.demos.web.entity.Property;
import com.example.carbon.demos.web.entity.Result;
import com.example.carbon.demos.web.service.PropertyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wms
 * @since 2024-09-10
 */
@RestController
@RequestMapping("/property")
public class PropertyController {
    @Autowired

    PropertyService propertyService;
    @PostMapping("/save")
    public Result save(@RequestBody Property property)
    {
        System.out.println("新增被调用");
        System.out.println(property.toString());

        return propertyService.save(property)?Result.success():Result.fail();
    }
    @PostMapping("/update")
    public Result update(@RequestBody Property  property)
    {
        System.out.println("修改被调用");
        System.out.println(property.toString());
        return propertyService.updateById(property)?Result.success():Result.fail();
    }
    @GetMapping("/delete")
    public Result delete(@RequestParam String id){
        System.out.println("删除被调用");
        return propertyService.removeById(id)?Result.success():Result.fail();
    }

    @PostMapping("/listPageWrapper")
    public Result listPageWrapper(@RequestBody QueryPageParam queryPageParam){
        System.out.println("被调用");

        HashMap hashMap=queryPageParam.getParam();

        //TODO 页实体
        Page<Property> page = new Page<Property>(queryPageParam.getPageNum(),queryPageParam.getPageSize());

        //TODO 根据数据name进行匹配
        LambdaQueryWrapper<Property> lambdaQueryWrapper=new LambdaQueryWrapper();


        String propertytype=(String)hashMap.get("propertytype");
        String propertycome=(String)hashMap.get("propertycome");


        if(!"null".equals(propertytype)&&!StringUtils.isEmpty(propertytype)) {
            lambdaQueryWrapper.eq(Property::getPropertytype, propertytype);
        }
        if(!"null".equals(propertycome)&&!StringUtils.isEmpty(propertycome)) {
            lambdaQueryWrapper.eq(Property::getPropertycome, propertycome);
        }


        IPage result = propertyService.pageWrapper(page,lambdaQueryWrapper);

        return Result.success(result.getRecords(),result.getTotal());
    }
}
